// components/mylist2/index2.js
const http = require('../../utils/http.js')
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        itemList: Object,
        xsqgItem: Object,
        num: Number,//显示样式
        gw: Number,//购物车显示0
        qyk: Number//权益卡
    },
    lifetimes: {
      attached: function() {
        this.properties.xsqgItem.type
        this.countdown(this.data.timeText)
      }
    },
    pageLifetimes: {
      show: function() {
        // 页面被展示
        
      }
    },

    /**
     * 组件的初始数据
     */
    data: {
      timeText:null,
      boll:true
    },

    /**
     * 组件的方法列表
     */
    methods: {
      // 倒计时
      countdown(time) {
        //console.log((this.properties.xsqgItem.beginTime).gettime)
        //console.log()
        var that=this
        var timenum=time
        var timetext=null
        //Math.floor((new Date(this.properties.xsqgItem.beginTime).getTime() > new Date(this.properties.xsqgItem.nowTime).getTime()) / 1000)
        if(new Date(this.properties.xsqgItem.beginTime.replace(/-/g, '/')).getTime() > new Date(this.properties.xsqgItem.nowTime.replace(/-/g, '/')).getTime()){
          //console.log(Math.floor((new Date(this.properties.xsqgItem.beginTime).getTime() > new Date(this.properties.xsqgItem.nowTime).getTime()) / 1000))
          // that.setData({
          //   timeText:'活动未开始'
          // })
          if(that.data.boll){
            timenum = Math.floor((new Date(this.properties.xsqgItem.beginTime.replace(/-/g, '/')).getTime() - new Date(this.properties.xsqgItem.nowTime.replace(/-/g, '/')).getTime()) / 1000)
            that.setData({
              boll:false
            })
          }
        }
        if(new Date(this.properties.xsqgItem.endTime.replace(/-/g, '/')).getTime() < new Date(this.properties.xsqgItem.nowTime.replace(/-/g, '/')).getTime()){
          that.setData({
            timeText:'活动已结束'
          })
          return false
        }
        if(new Date(this.properties.xsqgItem.beginTime.replace(/-/g, '/')).getTime() < new Date(this.properties.xsqgItem.nowTime.replace(/-/g, '/')).getTime() && new Date(this.properties.xsqgItem.endTime.replace(/-/g, '/')).getTime() > new Date(this.properties.xsqgItem.nowTime.replace(/-/g, '/')).getTime()){
          //console.log('OK')
          that.setData({
            timeText:''
          })
          return false
        }
        //console.log(timenum)
        // 未开始限时抢购
        if(this.data.timenum<=0){
          that.setData({
            timeText:''
          })
          return false
        }
        setTimeout(function () {
          timenum--
          that.setData({
            timeText:timenum
          })
          that.countdown(timenum)
        }, 1000)
        timetext=this.dateformat(timenum);
        //console.log(timetext)
        //this.data.timeText=timetext
        this.setData({
          timeText:timetext
        })
        
        //console.log(timenum)
        
        //console.log(timetext)
      },

      // 时间格式化输出，如11天03小时25分钟19秒  每1s都会调用一次
      dateformat(micro_second) {
        //console.log('dateformat')
        // 总秒数
        var second = micro_second;
        // 天数
        var day = Math.floor(second / 3600 / 24);
        // 小时
        var hr = Math.floor(second / 3600 % 24);
        // 分钟
        var min = Math.floor(second / 60 % 60);
        // 秒
        var sec = Math.floor(second % 60);
        return day + "天" + hr + ":" + min + ":" + sec;
      },
        itemClick(e) {
            let item = e.currentTarget.dataset;
            //console.log(e.currentTarget.dataset.type)
            if (this.properties.xsqgItem.activityType == 1) {
                http.get('/api/purchase/now').then(res => {
                  if (res.code == 200) {
                    if(new Date(this.properties.xsqgItem.beginTime.replace(/-/g, '/')).getTime() < new Date(res.data.replace(/-/g, '/')).getTime() && new Date(this.properties.xsqgItem.endTime.replace(/-/g, '/')).getTime() > new Date(res.data.replace(/-/g, '/')).getTime()){
                      wx.navigateTo({
                          url: '/pages/home/couponDetail/index?stockId=' + item.stockId
                      });
                    }else{
                      wx.showToast({
                          title: '当前活动未开始或已结束',
                          icon: 'none',
                      })
                    }
                  } 
                  
                })
                // http.get('/api/index/checkActivity', {activityId: this.properties.xsqgItem.id}).then(res => {
                //     if (res.code == 200) {
                //         wx.navigateTo({
                //             url: '/pages/home/couponDetail/index2?stockId=' + item.stockId
                //         });
                //     } else {
                //         wx.showToast({
                //             title: res.message,
                //             icon: 'none',
                //         })
                //     }
                // })
            } else {
                if(this.properties.qyk==1){
                    wx.navigateTo({
                        url: '/pages/home/equityCard/index?id=' + item.stockId + '&qyk=' + this.properties.qyk
                    });
                }else{
                    wx.navigateTo({
                        url: '/pages/home/couponDetail/index?stockId=' + item.stockId
                    });
                }
            }
        },
        // 
        imgClick(e){
            //console.log('123')
            let item = e.currentTarget.dataset;
            if(item.transurl){
                wx.navigateTo({
                    url: '/pages/home/couponDetail/index?stockId=' + item.transurl
                });
            }
        }
    }
})
